package com.mmall.controller.backend;

/**
 * Created by sc on 2017/9/9.
 */

import com.github.pagehelper.PageInfo;
import com.mmall.common.Const;
import com.mmall.common.ResponseCode;
import com.mmall.common.ServiceResponse;
import com.mmall.pojo.User;
import com.mmall.service.IOrderService;
import com.mmall.service.IUserService;
import com.mmall.vo.OrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;


@Controller
@RequestMapping("/manage/order")
public class OrderManageController {

    @Autowired
    private IUserService iUserService;

    @Autowired
    private IOrderService iOrderService;

    @RequestMapping("list.do")
    @ResponseBody
    public ServiceResponse<PageInfo> orderList(HttpSession session, @RequestParam(value = "pagenum", defaultValue = "1") Integer pagenum, @RequestParam(value = "pagesize", defaultValue = "10") Integer pagesize) {
        User user = (User) session.getAttribute(Const.Current_User);
        if (user == null) {
            return ServiceResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), "用户未登录");
        }
        if (iUserService.checkAdminRole(user).inSuccess()) {
            return iOrderService.manageList(pagenum, pagesize);
        } else {
            return ServiceResponse.createByErrorMessage("无权限操作");
        }
    }

    @RequestMapping("detail.do")
    @ResponseBody
    public ServiceResponse<OrderVo> OrderDetail(HttpSession session, Long orderNo) {
        User user = (User) session.getAttribute(Const.Current_User);
        if (user == null) {
            return ServiceResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), "用户未登录");
        }
        if (iUserService.checkAdminRole(user).inSuccess()) {
            return iOrderService.manageDetail(orderNo);
        } else {
            return ServiceResponse.createByErrorMessage("无权限操作");
        }
    }

    @RequestMapping("search.do")
    @ResponseBody
    public ServiceResponse<PageInfo> OrderSearch(HttpSession session, Long orderNo, @RequestParam(value = "pagenum", defaultValue = "1") Integer pagenum, @RequestParam(value = "pagesize", defaultValue = "10") Integer pagesize) {
        User user = (User) session.getAttribute(Const.Current_User);
        if (user == null) {
            return ServiceResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), "用户未登录");
        }
        if (iUserService.checkAdminRole(user).inSuccess()) {
            return iOrderService.search(orderNo, pagenum, pagesize);
        } else {
            return ServiceResponse.createByErrorMessage("无权限操作");
        }
    }


    @RequestMapping("send_goods.do")
    @ResponseBody
    public ServiceResponse<String> sendGoods(HttpSession session, Long orderNo) {
        User user = (User) session.getAttribute(Const.Current_User);
        if (user == null) {
            return ServiceResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), "用户未登录");
        }
        if (iUserService.checkAdminRole(user).inSuccess()) {
            return iOrderService.sendGoods(orderNo);
        } else {
            return ServiceResponse.createByErrorMessage("无权限操作");
        }
    }

}
